package com.sbw.springcloud.client.demo.controller;

import com.sbw.springcloud.client.demo.service.HelloClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: sangbowei
 * @Description: TODO
 * @Date: Created in  17:51 2017\12\26 0026
 **/
@RestController
public class HelloController {

    Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    HelloClientService helloClientService;

    @Value("${server.port}")
    String port;

    @GetMapping("/discovery_client")
    public String discoveryClient() {
        String services = "Services: " + discoveryClient.getServices();
        String log = "discovery_client result is "+services +" , port is "+port;
        logger.info(log);
        return log;
    }

    @GetMapping("/hello")
    public String hello() {
        String log = "hello world 1 ! port is "+port;
        logger.info(log);
        return log;
    }

    @GetMapping("/hello_delay")
    public String helloDelay() {
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String log = "hello world delay 5s ! port is "+port;
        logger.info(log);
        return log;
    }

    @GetMapping("/hello_params")
    public String helloParams(@RequestParam String name){
        String log = "hello, this is response from eureka-client,name is "+name+", port is "+port;
        logger.info(log);
        return log;
    }

    @GetMapping("/hello_client")
    public String helloClient() {
        String log = "hello client service result is "+ helloClientService.hello()+" ; port is "+port;
        logger.info(log);
        return log;
    }

}
